package com.heima.article.listener;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class UpDownListener {

    @Autowired
    private IApArticleService apArticleService;

    //指定监听主题
    @KafkaListener(topics = "${topic.upDownTopic}")
    public void handleMessage(ConsumerRecord<String,String> record){
        //获取key
       String key=record.key();
       //获取value
       String value=record.value();
       //获取当前的状态
        long articleId = Long.parseLong(key);
       Integer isDown= value.equals("1")?0 :1;
       //输出接收到的信息
        System.out.println("接收到上下架消息: articleId = " + articleId + " , isDown = " + isDown);
        //构建查询修改条件
        LambdaUpdateWrapper<ApArticle> query = new LambdaUpdateWrapper<>();
        //根据id
        query.eq(ApArticle::getId,articleId);
        //修改状态
        query.set(ApArticle::getIsDown,isDown);
        apArticleService.update(query);

    }
}
